import QtQuick 1.0
import com.nokia.symbian 1.1
import "../delegates"
import "../js/core.js" as Core

Page {
    id: root

    property string quizTopicName
    property int quizTopicID
    property int ans
    property int score;

    TitleHeader {
        id: header;
        text: "Quiz - " + quizTopicName + " (Score: " + score + ")"
    }

    Text{
        id: question
        anchors.horizontalCenter: parent.horizontalCenter
        font.pixelSize: parent.height*0.04
        anchors.top: parent.top
        anchors.topMargin: parent.height * 0.1
        color: "white"
    }

    Button{
        id: ansA
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: question.bottom;
        anchors.topMargin: parent.height * 0.1;
        height: parent.height * 0.125
        width: parent.height * 0.5
        onClicked: buttonPressed(0);
    }

    Button{
        id: ansB
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: ansA.bottom;
        anchors.topMargin: parent.height * 0.05;
        height: parent.height * 0.125
        width: parent.height * 0.5
        onClicked: buttonPressed(1);
    }

    Button{
        id: ansC
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: ansB.bottom;
        anchors.topMargin: parent.height * 0.05;
        height: parent.height * 0.125
        width: parent.height * 0.5
        onClicked: buttonPressed(2);
    }

    Button{
        id: ansD
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.top: ansC.bottom;
        anchors.topMargin: parent.height * 0.05;
        height: parent.height * 0.125
        width: parent.height * 0.5
        onClicked: buttonPressed(3);
    }

    Image{
        id:correct
        source: "images/correct.png"
        anchors.centerIn: root
        width: root.width*0.7
        height:root.width*0.7
        opacity: 0.0

        SequentialAnimation{
            id: correctAnimation
            NumberAnimation{
                target: correct
                property:"opacity"
                from: 0.0
                to: 1.0
                duration:300
                easing.type: "InCubic"
            }
            NumberAnimation{
                duration:600
            }
            NumberAnimation{
                target: correct
                property:"opacity"
                from: 1.0
                to: 0.0
                duration:300
                easing.type: "OutCubic"
            }
        }
    }

    Image{
        id:incorrect
        source:"images/incorrect.png"
        anchors.centerIn:root
        width: root.width*0.7
        height:root.width*0.7
        opacity: 0.0

        SequentialAnimation{
            id: incorrectAnimation
            NumberAnimation{
                target: incorrect
                property:"opacity"
                from: 0.0
                to: 1.0
                duration:300
                easing.type: "InCubic"
            }
            NumberAnimation{
                duration:600
            }
            NumberAnimation{
                target: incorrect
                property:"opacity"
                from: 1.0
                to: 0.0
                duration:300
                easing.type: "OutCubic"
            }
        }
    }

    tools: ToolBarLayout {
        ToolButton{
            iconSource: "toolbar-back"
            onClicked:
                root.pageStack.pop();
        }
    }

    function buttonPressed(btnNum){
        if (btnNum == ans){
            correctAnimation.running = true;
            newQuestion();
            score++;
        }
        else{
            incorrectAnimation.running = true;
            score = 0;
        }
    }

    function newQuestion(){
        ans = Core.readQuestionsForQuiz(quizTopicID, question, ansA, ansB, ansC, ansD);
    }

    onStatusChanged: {
        if(status == PageStatus.Activating) {
            newQuestion();
            score = 0;
        }
    }
}
